# !/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2020/12/17 21:36
# @Author  : darknight
# @File    : parser.py
# @Software: PyCharm

from core.parser import BaseParse
from settings import settings


class Parser(BaseParse):

    @staticmethod
    def parser_recommend_songs_list(source, result) -> list:
        data = [dict(
            id=item['id'],
            title=item['name'],
            pic=item['picUrl'],
            listen_num=item['playCount'],
            source=source,
        ) for item in result['result']]
        return data[0: Parser.LIMIT]

    @staticmethod
    def parser_recommend_songs(source, result) -> list:
        result = result['result']
        data = [dict(
            id=item['id'],
            mid=item['id'],
            songName=item['name'],
            singer=[singer['name'] for singer in item['song']['artists']],
            vid=item['song']['mvid'],
            pic=item['picUrl'],
            source=source,
        ) for item in result]
        return data[0:Parser.LIMIT]

    @staticmethod
    def parser_songs_list_by_category(source, result) -> list:
        data = [dict(
            id=item['id'],
            pic=item['coverImgUrl'],
            listen_num=item['playCount'],
            title=item['name'],
            source=source,
        ) for item in result['playlists']]
        return data[0:Parser.LIMIT]

    @staticmethod
    def parser_songs_list_detail(source, result) -> dict:
        detail = result['result']
        songs = detail['tracks']
        data = dict(
            id=detail['id'],
            logo=detail['coverImgUrl'],
            title=detail['name'],
            desc=detail['description'],
            listen_num=detail['playCount'],
            creator=detail['creator']['nickname'],
            source=source,
            songs=[
                dict(
                    id=song['id'],
                    mid=detail['id'],
                    songName=song['name'],
                    singer=[singer['name'] for singer in song['artists']],
                    vid=song['mvid'],
                    pic=song['album']['picUrl'],
                    source=source,
                ) for song in songs
            ]
        )
        return data

    @staticmethod
    def parser_song_detail(source, result) -> dict:
        res = result['songs'][0]
        data = dict(
            id=res['id'],
            mid=res['id'],
            songName=res['name'],
            singer=[item['name'] for item in res['ar']],
            vid=res['mv'],
            pic=res['al']['picUrl'],
            duration=res['dt'],
            source=source,
        )
        return data

    @staticmethod
    def parser_song_play_url(source, result) -> str:
        data = result['data'][0]['url']
        return data

    @staticmethod
    def parser_song_lyric(source, result) -> str:
        data = result['lrc']['lyric']
        return data

    @staticmethod
    def parser_mv_play_url(source, result) -> list:
        data = [{settings.VIDEO_QUALITY[k]: v} for item in result for k, v in item.items()]
        return data

    @staticmethod
    def parser_top_list(source, result) -> dict:
        result = result['result']
        data = dict(
            id=result['id'],
            logo=result['coverImgUrl'],
            title=result['name'],
            desc=result['description'],
            listen_num=result['playCount'],
            source=source,
            songs=[dict(
                id=song['id'],
                mid=song['id'],
                songName=song['name'],
                singer=[singer['name'] for singer in song['artists']],
                vid=song['mvid'],
                pic=song['album']['picUrl'],
                source=source,
            ) for song in result['tracks']],
        )
        return data

    @staticmethod
    def parser_search_song(source, result) -> list:
        songs = result['result']['songs']
        data = [dict(
            id=song['id'],
            mid=song['id'],
            songName=song['name'],
            singer=[item['name'] for item in song['ar']],
            vid=song['mv'],
            pic=song['al']['picUrl'],
            source=source,
        ) for song in songs]
        return data[0: Parser.LIMIT]
